#include "inputitemdelegate.h"

#include <QComboBox>
#include <QSerialPortInfo>
#include <QSpinBox>

inputItemDelegate::inputItemDelegate(int col_port,QObject *parent)
{
    this->col_port=col_port;
}

void inputItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyledItemDelegate::paint(painter, option, index);
}

QWidget *inputItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //QStringList
    if(index.column()==col_port)
    {
        QComboBox *cb=new QComboBox(parent);
        foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
        {
                cb->addItem(info.portName());

        }
        return cb;
    }
    else
         return QStyledItemDelegate::createEditor(parent, option, index);
}

void inputItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    int strindex=-1;
    if (index.column() == col_port)
    {
        QComboBox *cb=qobject_cast<QComboBox *>(editor);
        strindex=cb->findText(index.model()->data(index,Qt::DisplayRole).toString());
        cb->setCurrentIndex(strindex);
    }
    else
    {
        QStyledItemDelegate::setEditorData(editor, index);
    }
}

void inputItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if (index.column() == col_port)
    {
        QComboBox *cb=qobject_cast<QComboBox *>(editor);
        model->setData(index, cb->currentText());
    }
    else
    {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
}

void inputItemDelegate::setPortList(QStringList ports)
{
    this->portlist=ports;
}
